var app = angular.module('myapp', []);
app.controller('changepwd.ctrl', ['$scope', '$http', function ($scope, $http) {
    $scope.ChangePwd = function () {
        $http.get('/user/isLogin').then(function (res) {
            if (res.data.status) {
                var username = res.data.data[0].username;
                $http.post('/user/getuser',{username:username,password:$scope.prePwd}).
                then(function (res) {
                    if (res.data.status){
                        $scope.prePwderr=false;
                        if ($scope.newPwd==$scope.rePwd&&$scope.newPwd!=null){
                            $scope.err=false;
                            var user = {
                                username: username,
                                password: $scope.newPwd
                            };
                            $http.post('/user/updateinfo', user).
                            then(function (res) {
                                if (res.data.msg=='success'){
                                    $scope.success=true;
                                    $http.get('/user/loginoff').
                                        then(function () {
                                        setInterval(function () {
                                            location.href="../login.html";
                                        },300);
                                    });
                                }else{
                                    $scope.success=false;
                                }
                            })
                        }else{
                            $scope.err=true;
                        }
                    }else{
                        $scope.prePwderr=true;
                    }
                })
            }
        })
    };
}]);
